package com.woniuxy.userassembly.controller;

import com.woniu.cloud.StoreAPI;
import com.woniu.cloud.api.GoodApi;
import com.woniu.cloud.api.StoreApi;
import com.woniu.cloud.api.StoreGoodApi;
import com.woniu.cloud.api.serve.ServeApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.FirstService;
import com.woniu.cloud.entity.SecondService;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.entity.TStore;
import com.woniuxy.api.GoodOrderApi;
import com.woniuxy.api.ScoreOrderApi;
import com.woniuxy.api.ServiceApi;
import com.woniuxy.api.ServiceOrderApi;
import com.woniuxy.cloud.UserAPI;
import com.woniuxy.cloud.entity.TGoodCar;
import com.woniuxy.cloud.entity.TServiceCar;
import com.woniuxy.cloud.entity.TUserServiceProcess;
import com.woniuxy.cloud.form.AddGoodCarForm;
import com.woniuxy.cloud.form.AddServiceCarForm;
import com.woniuxy.entity.GoodOrder;
import com.woniuxy.entity.ScoreOrder;
import com.woniuxy.entity.ServiceOrder;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.PersonalInformationForm;
import com.woniuxy.userassembly.util.JwtUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description:
 * @author: ZXG
 * @time: 2021/9/23
 */
@RestController
@Slf4j
@Api(tags = "用户服务管理", description = "提供用户相关的 API")
public class ServiceApiImp implements ServiceApi {
  @Resource
   private UserAPI userAPI;

  @Resource
   private ServiceOrderApi serviceOrderApi;
  @Resource
   private GoodOrderApi goodOrderApi;
  @Resource
   private ScoreOrderApi scoreOrderApi;
  @Resource
  private StoreApi storeAPI;
  @Resource
  private GoodApi goodApi;
  @Resource
  private ServeApi serveApi;


    @GetMapping("/getUserServiceProcessListByUserId")
    @ApiOperation("根据服务订单id查看用户服务进度")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceOrderDetailId", value = "订单id", dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TUserServiceProcess.class)
    })
    @Override
    public Result getUserServiceProcessListByUserId(Integer serviceOrderDetailId) {
        log.info("用户服务订单={}",serviceOrderDetailId);

        return  userAPI.getUserServiceByUserServiceProcessId(serviceOrderDetailId);
    }


    @PutMapping("/updateServiceOrderDetail")
    @ApiOperation("开始服务,修改服务详细订单订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceOrderDetailId", value = "服务详情订单id", dataType = "Integer"),
            @ApiImplicitParam(name = "status", value = "服务详情订单状态", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Boolean.class)
    })
    @Override
    public Result updateServiceOrderDetail(Integer serviceOrderDetailId) {
       return serviceOrderApi.updateServiceOrderStatus(serviceOrderDetailId,1);
    }


    @GetMapping("/getPersonalInformation")
    @ApiOperation("根据用户id查询个人信息")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = PersonalInformationForm.class)
    })
    @Override
    public Result getPersonalInformation() {
      Integer userId = JwtUtil.getUserId();
      return userAPI.getUserByUserId(userId);
    }



    @GetMapping("/getFirstService")
    @ApiOperation("查询一级服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = FirstService.class)
    })
    @Override
    public Result getFirstService(Integer pageIndex, Integer pageSize) {
        return serveApi.getAllFirstService(pageIndex,pageSize);
    }




    @GetMapping("/getSecondService")
    @ApiOperation("根据一级服务id查询二级服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstServiceId", value = "一级服务id", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = SecondService.class)
    })
    @Override
    public Result getSecondService(Integer firstServiceId) {
      return serveApi.getSecondServiceByFirstServiceId(firstServiceId);
    }



    @GetMapping("/getGoodCarByUserId")
    @ApiOperation("根据用户id查询商品购物车列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TGoodCar.class)
    })
    @Override
    public Result getGoodCarByUserId() {
      Integer userId = JwtUtil.getUserId();
      return userAPI.getGoodCarListByUserId(userId);

    }



    @GetMapping("/getServiceCarByUserId")
    @ApiOperation("根据用户id查询服务购物车列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TServiceCar.class)
    })
    @Override
    public Result getServiceCarByUserId() {
      Integer userId = JwtUtil.getUserId();
        return userAPI.getServiceCarListByUserId(userId);
    }



    @GetMapping("/getGoodOrderByUserId")
    @ApiOperation("根据用户id查询商品订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = GoodOrder.class)
    })
    @Override
    public Result getGoodOrderByUserId(Integer pageIndex) {
      Integer userId = JwtUtil.getUserId();
      return goodOrderApi.getGoodOrderByUserId(pageIndex,5,userId);
    }


    @GetMapping("/getServiceOrderByUserId")
    @ApiOperation("根据用户id查询服务订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getOrder", value = "查询服务订单参数", dataType = "GetOrder"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ServiceOrder.class)
    })
    @Override
    public Result getServiceOrderByUserId(GetOrder getOrder) {
        return serviceOrderApi.getServiceOrderByUserId(getOrder);
    }



    @GetMapping("/getScoreOrderByUserId")
    @ApiOperation("根据用户id查询积分订单列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ScoreOrder.class)
    })
    @Override
    public Result getScoreOrderByUserId(GetOrder getOrder) {
     return scoreOrderApi.getScoreOrderByUserId(getOrder);
    }


    @GetMapping("/getGoodsList")
    @ApiOperation("查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodName", value = "商品名称", dataType = "String"),
            @ApiImplicitParam(name = "goodType", value = "商品类别", dataType = "String"),
            @ApiImplicitParam(name = "pageIndex", value = "当前页", dataType = "Integer")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TGood.class)
    })
    @Override
    public Result getGoodsList(String goodName,String goodType, Integer pageIndex) {
      log.info("商品名称={},商品类别={},当前页={}",goodName,goodType,pageIndex);
      if(goodName.isEmpty()){
        if(goodType.isEmpty()){
          return Result.fail("查询内容为空",null);
        }
        return goodApi.getGoodByGoodType(goodType,pageIndex);
      }
      return goodApi.getGoodByGoodName(goodName,pageIndex);
    }


    @GetMapping("/getStoreByStoreId")
    @ApiOperation("根据店铺id查询店铺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺id", dataType = "Integer")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TStore.class)
    })
    @Override
    public Result getStoreByStoreId(Integer storeId) {
       log.info("店铺id={}",storeId);
       return storeAPI.getStoreByStoreId(storeId);
    }


    @GetMapping("/getNearbyShops")
    @ApiOperation("根据经纬度查询附近店铺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeLongitude", value = "用户所在的经纬度", dataType = "Double"),
            @ApiImplicitParam(name = "storeLatitude", value = "店铺所在的经纬度", dataType = "Double"),
            @ApiImplicitParam(name = "radius", value = "附近范围", dataType = "Double")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TStore.class)
    })
    @Override
    public Result getNearbyShops(Double storeLongitude, Double storeLatitude,Double radius) {
        return  storeAPI.getStoreList(storeLatitude,storeLatitude,radius);
    }


    @PutMapping("/updateGoodOrder")
    @ApiOperation("确认收货修改商品订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodOrderId", value = "商品订单id", dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Boolean.class)
    })
    @Override
    public Result updateGoodOrder(Integer goodOrderId) {
        return goodOrderApi.updateGoodOrderStatusById(goodOrderId,6);
    }



  @PostMapping("/insertGoodCar")
  @ApiOperation("新增商品购物车")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "addGoodCarForm", value = "新增商品购物车参数", dataType = "AddGoodCarForm"),
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "OK", response = Boolean.class)
  })
  @Override
  public Result insertGoodCar(AddGoodCarForm addGoodCarForm) throws NoSuchFieldException {
    return userAPI.insertGoodCar(addGoodCarForm);
  }



  @PostMapping("/insertServiceCar")
  @ApiOperation("新增服务购物车")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "addServiceCarForm", value = "新增服务购物车参数", dataType = "AddServiceCarForm"),
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "OK", response = Boolean.class)
  })
  @Override
  public Result insertServiceCar(AddServiceCarForm addServiceCarForm) {
    return userAPI.insertServiceCar(addServiceCarForm);
  }


}
